import os
import sys
# 获取项目根目录路径（your_project/ 的绝对路径）
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(project_root)  # 将项目根目录加入模块搜索路径
from data_handle import Run
import subprocess
import sys
import time
from threading import Timer


# if __name__ == '__main__':
#     aa = Run()
#     aa.bb_3(0, 50)


# if __name__ == "__main__":
#     restart = True
#     try:
#         aa = Run()
#         aa.bb_3(0, 50)
#
#     except KeyboardInterrupt:
#         print("\n用户终止操作，程序退出。")
#         restart = False
#     except Exception as e:
#         print(f"程序运行出错: {e}")
#         restart = False
#     finally:
#         if restart:
#             print("正在启动新实例...")
#             subprocess.Popen([sys.executable, sys.argv[0]])
#         sys.exit()

def restart_program():
    print("重...")
    time.sleep(1)
    os.execv(sys.executable, [sys.executable] + sys.argv)



if __name__ == "__main__":
    # 每小时（3600秒）触发一次重启
    a = Timer(10, restart_program)
    a.start()
    restart = True
    try:
        aa = Run()
        aa.bb_3(0, 50)
    except KeyboardInterrupt:
        print("\n用户终止操作，程序退出。")
        restart = False
    except Exception as e:
        print(f"程序运行出错: {e}")
        restart = False
    finally:
        if not restart:
            a.join()
    # finally:
    #     if restart:
    #         print("正在启动新实例...")
    #         subprocess.Popen([sys.executable, sys.argv[0]])
    #     sys.exit()
